var api = require('../../../config/api.js');
var check = require('../../../utils/check.js');

var app = getApp();
Page({
    data: {
        username: '',
        password: '',
        confirmPassword: '',
        mobile: '',
        code: '',
        statusBarHeight: 0,
        countdown: 60, // 倒计时初始值
        isCounting: false // 是否正在倒计时
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        // 页面渲染完成
        const {statusBarHeight} = wx.getSystemInfoSync();
        this.setData({statusBarHeight});
    },
    onReady: function () {

    },
    onShow: function () {
        // 页面显示

    },
    onHide: function () {
        // 页面隐藏

    },
    onUnload: function () {
        // 页面关闭

    },
    navBack: function () {
        wx.navigateBack({})
    },
    sendCode: function () {
        let that = this;
        if (this.data.isCounting) return; // 如果正在倒计时，直接返回
        if (this.data.mobile.length == 0) {
            // wx.showModal({
            //   title: '错误信息',
            //   content: '手机号不能为空',
            //   showCancel: false
            // });
            wx.showToast({
                title: '手机号不能为空',
                icon: 'none'
            })
            return false;
        }
        if (!check.isValidPhone(this.data.mobile)) {
            wx.showToast({
                title: '手机号格式不正确',
                icon: 'none'
            })
            return false;
        }

        wx.request({
            url: api.AuthRegisterCaptchaNew,
            data: {
                mobile: that.data.mobile
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.errno == 0) {
                    wx.showModal({
                        title: '发送成功',
                        content: '验证码已发送',
                        showCancel: false
                    });
                    that.setData({
                        isCounting: true
                    });
                    let interval = setInterval(function () {
                        that.setData({
                            countdown: that.data.countdown - 1
                        });
                        if (that.data.countdown <= 0) {
                            that.setData({
                                isCounting: false,
                                countdown: 60
                            });
                            clearInterval(interval);
                        }
                    }, 1000);
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.data.errmsg,
                        showCancel: false
                    });
                }
            }
        });
    },
    requestRegister: function (wxCode) {
        let that = this;
        wx.request({
            url: api.AuthRegister,
            data: {
                username: that.data.username,
                password: that.data.password,
                mobile: that.data.mobile,
                code: that.data.code,
                wxCode: wxCode
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.errno == 0) {
                    app.globalData.hasLogin = true;
                    wx.setStorageSync('userInfo', {
                        ...res.data.data.userInfo,
                        goldcoin: res.data.data.goldcoin
                    });
                    wx.setStorage({
                        key: "token",
                        data: res.data.data.token,
                        success: function () {
                            wx.switchTab({
                                url: '/pages/ucenter/index/index'
                            });
                        }
                    });
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.data.errmsg,
                        showCancel: false
                    });
                }
            }
        });
    },
    startRegister: function () {
        var that = this;
        if (!this.data.username) {
            wx.showToast({
                title: '用户名不能为空',
                icon: 'none'
            })
            return
        }
        if (this.data.password.length < 6) {
            wx.showToast({
                title: '密码不得少于6位',
                icon: 'none'
            })
            return false;
        }

        if (this.data.password != this.data.confirmPassword) {
            wx.showToast({
                title: '确认密码不一致',
                icon: 'none'
            })
            return false;
        }

        if (this.data.mobile.length == 0) {
            wx.showToast({
                title: '手机号不能为空',
                icon: 'none'
            })
            return false;
        }
        if (!check.isValidPhone(this.data.mobile)) {
            wx.showToast({
                title: '手机号格式不正确',
                icon: 'none'
            })
            return false;
        }

        if (this.data.code.length == 0) {
            wx.showToast({
                title: '验证码不能为空',
                icon: 'none'
            })
            return false;
        }

        wx.login({
            success: function (res) {
                if (!res.code) {
                    wx.showModal({
                        title: '提示',
                        content: '注册失败',
                        showCancel: false
                    });
                }

                that.requestRegister(res.code);
            }
        });
    },
    bindUsernameInput: function (e) {

        this.setData({
            username: e.detail.value
        });
    },
    bindPasswordInput: function (e) {

        this.setData({
            password: e.detail.value
        });
    },
    bindConfirmPasswordInput: function (e) {

        this.setData({
            confirmPassword: e.detail.value
        });
    },
    bindMobileInput: function (e) {

        this.setData({
            mobile: e.detail.value
        });
    },
    bindCodeInput: function (e) {

        this.setData({
            code: e.detail.value
        });
    },
    clearInput: function (e) {
        switch (e.currentTarget.id) {
            case 'clear-username':
                this.setData({
                    username: ''
                });
                break;
            case 'clear-password':
                this.setData({
                    password: ''
                });
                break;
            case 'clear-confirm-password':
                this.setData({
                    confirmPassword: ''
                });
                break;
            case 'clear-mobile':
                this.setData({
                    mobile: ''
                });
                break;
            case 'clear-code':
                this.setData({
                    code: ''
                });
                break;
        }
    }
})
